<script>
    // 进销存 仓库管理表单
    Vue.component('product_stock_case', {
        template: `
<el-dialog :visible.sync="dialogShow" width="600px" @close="closeModal">
    <h3 slot="title">
        <span v-if="houseForm.id">编辑</span>
        <span v-else>新增</span> 单位
    </h3>
    <el-form ref="form" :rules="formRules" :model="houseForm" label-width="80px">
    <el-form-item v-if="houseForm.id" class="hidden">
    <el-input v-model="houseForm.id" disabled></el-input>
    </el-form-item>
    <el-form-item prop="title" label="单位名称">
    <el-input autocomplete="off" v-model="houseForm.product_case_name" placeholder="请输入单位名称"></el-input>
    </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
    <el-button @click="closeModal">取 消</el-button>
    <el-button type="primary" @click="handleFormSubmit">确 定</el-button>
    </div>
</el-dialog>
    `,
        props: ['dialog-show','form-row'],
        computed: {
            currentTallyId() {
                return this.$store.state.currentTallyId
            },
            currentTally () {
                return this.$store.state.currentTally
            },
        },
        watch: {
            dialogShow:{
                handler (newVal) {},
                deep:true,
                immediate: true
            },
            formRow: {
                handler (newVal) {
                    this.houseForm = newVal
                },
                deep:true,
                immediate: true
            }
        },
        data:function(){
            return {
                houseForm:{},
                formRules:{
                    product_case_name: [
                        { required: true, message: '请输入单位名称', trigger: 'blur' }
                    ],
                },
            }
        },
        methods:{
            closeModal(){
                this.$emit('houseModalClose','')
            },

            handleFormSubmit(){
                let that = this
                let postUrl;
                if(that.houseForm.id) {
                    postUrl = '/api/product-stock-case/edit'
                }else{
                    postUrl = '/api/product-stock-case/add'
                }
                that.houseForm.tally_id = that.currentTallyId
                axios.post(postUrl,that.houseForm,{
                    headers: {'token': loginToken},
                }).then(function(res) {
                    if(res.data.status === 'success'){
                        that.$message({
                            type: 'success',
                            message: '数据保存成功'
                        });
                        that.houseForm = {}
                        that.$emit('houseModalClose','refresh')
                    }else{
                        that.$message({
                            type: 'error',
                            message: '解析数据失败:'+res.data.error_message
                        })
                    }
                })
            },
        }
    })
</script>
